<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 支付宝请求控制器
 * @package  onlinepay
 * @author   allen.qiang
 * @version 1.0
 */

quanli_include('module/base/common/quanliOnlinepay.class.php');
quanli_include('module/base/common/quanliXml.class.php');
quanli_include('module/base/common/quanliHttp.class.php');

//实例化支付类
$onlinepayObj = new quanliOnlinepay();

//实例xml类
$quanliXml = new quanliXml();

//实例http类
$quanliHttp = new quanliHttp();

//设置网关
$onlinepayObj->setGateURL( $online_parameter_array['gate_url'] );



//设置参数
$params = array(
    'service' => 'alipay.wap.trade.create.direct',//接口名称
    'format' => 'xml', //请求参数格式
    'v' => '2.0', //接口版本号
    'partner' => $online_parameter_array['partnerID'],//合作伙伴ID
    'req_id' => $online_parameter_array['bill_no'], //请求号
    'sec_id' => 'MD5'
);
$onlinepayObj->setParamsArray( $params );


/**
 * ======================
 * 授权接口参数
 * ======================
 */

//授权接口请求业务参数
$req_data_array = array(
    'direct_trade_create_req' => array(
        'subject' => $online_parameter_array['bill_no'],//商品名称，必填
        'out_trade_no' => $online_parameter_array['bill_no'],//商品外部交易号，必填（保证唯一性）
        'total_fee' => $online_parameter_array['pay_amount'],//商品单价，必填（价格不能为0）
        'seller_account_name' => $online_parameter_array['seller_email'],//卖家邮箱，必填
        'call_back_url' => G_WEBSITE_DOMAINNAME . '/onlinepay/alipaywap/' . $online_parameter_array['subsite'] . '/clientReceive.html',//同步返回
        'notify_url' => G_WEBSITE_DOMAINNAME . '/onlinepay/alipaywap/' . $online_parameter_array['subsite'] . '/serverReceive.html',//异步返回
        'out_user' => $G_SHOP->currentPCustID //买家在商户系统的唯一标识
    )
);
$request_xml = $quanliXml->encode($req_data_array);
$onlinepayObj->setParameter('req_data', $request_xml);


//生成MD5签名字符串
$sign_str = $onlinepayObj->createSignStr();
$sign_str .= $online_parameter_array['md5_key'];
$onlinepayObj->setParameter('sign', md5($sign_str));

/**
 * ======================
 * 授权接口参数
 * ======================
 */


//获取授权令牌
$result_array = $quanliHttp->post( $online_parameter_array['gate_url'], $onlinepayObj->getAllParameters() );
if ($result_array['code'] == 200) {

    //解析字符串
    $receive_array = parseUrlParams( $result_array['data'] );
    if (isset($receive_array['res_data'])) {

        //获得access_token
        $res_data_array = $quanliXml->decode( $receive_array['res_data'] );


        /**
         * ======================
         * 交易接口
         * ======================
         */

        //交易接口请求业务参数
        $req_data_array = array(
            'auth_and_execute_req' => array(
                'request_token' => $res_data_array['request_token']
            )
        );
        $request_xml = $quanliXml->encode($req_data_array);
        $onlinepayObj->setParameter('req_data', $request_xml);

        $onlinepayObj->setParameter('service', 'alipay.wap.auth.authAndExecute');

        //生成MD5签名字符串
        $sign_str = $onlinepayObj->createSignStr( array( 'sign' ) );
        $sign_str .= $online_parameter_array['md5_key'];
        $onlinepayObj->setParameter('sign', md5($sign_str));


        /**
         * ======================
         * 交易接口
         * ======================
         */

        //保存请求参数信息
        $onlinepayObj->setSendParams($online_parameter_array['bill_no'], $online_parameter_array['payTypeID'], $online_parameter_array['payTypeName']);

        //生成请求URL
        $requestURL = $onlinepayObj->createRequestUrl();

    } else {

        $res_data_array = $quanliXml->decode( $receive_array['res_error'] );
        $pay_error = $req_data_array['msg'];

    }

} else {

    $pay_error = '支付调用失败';

}